package com.bear.newsset.mvp.presen;

import android.content.Context;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.bear.newsset.dao.sql.NewsDataSQL;
import com.bear.newsset.entity.NewsLableEntity;
import com.bear.newsset.entity.NewsListEntity;
import com.bear.newsset.entity.NewsParamEntity;
import com.bear.newsset.entity.SiteListEntity;
import com.bear.newsset.mvp.model.NewsModel;
import com.bear.newsset.mvp.view.HomeView;
import com.bear.newsset.util.DOM;
import com.bear.newsset.util.LogUtils;
import com.bear.newsset.widget.NewsListRecyclerView;
import com.bear.newsset.widget.NewsView;
import com.ybear.ybmvp.annotations.Model;
import com.ybear.ybmvp.enums.LIFE_STATUS;
import com.ybear.ybmvp.presenter.YBPresenter;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import static com.bear.newsset.util.Constant.DOM_ID_CALL_QUERY_NEWS_LABLE_LIST;
import static com.bear.newsset.util.Constant.DOM_ID_CALL_QUERY_SITE_LIST;
import static com.bear.newsset.util.Constant.DOM_ID_SITE_POSITION;

public class HomePresen extends YBPresenter<HomeView> implements DOM.OnResultListener {
    /**
     * 回调接口新闻来源列表
     */
    public interface CallBackSiteListListener {
        void result(String[][] siteList);
    }

    @Nullable
    private Context mContext;
    private NewsDataSQL mNewsSQL;
    private int sitePosition = 0;

    @Model
    private NewsModel newsModel;

    public HomePresen(@NonNull HomeView view) {
        super(view);
        mContext = view.getContext();
        mNewsSQL = NewsDataSQL.getInstance();
        DOM.getInstance().registerResult( this );
    }

    @Override
    public void onLifeStatus(LIFE_STATUS status) {
        super.onLifeStatus(status);
        if( status == LIFE_STATUS.DESTROY ) {
            DOM.getInstance().unRegisterResult( this );
        }
    }

    @Override
    public void onResult(int id, Object data) {
        switch ( id ) {
            case DOM_ID_CALL_QUERY_SITE_LIST:
                if( !(data instanceof  SiteListEntity) ) break;
                LogUtils.d("onResult", "查询来源列表");
                break;
            case DOM_ID_CALL_QUERY_NEWS_LABLE_LIST:
                if( !(data instanceof Map) ) break;
                LogUtils.d("onResult", "查询新闻类别列表");
                break;
            case DOM_ID_SITE_POSITION:
                if( data instanceof Integer ) sitePosition = (int) data;
                LogUtils.d("onResult", "分类位置：" + sitePosition);
                break;
        }
    }

    /**
     * 获取新闻来源列表
     * @param mCallBackSiteListListener         回调接口
     */
    public void getSiteList(final CallBackSiteListListener mCallBackSiteListListener) {
        newsModel.getSiteList(mCallBackSiteListListener :: result);
    }

    /**
     * 查询新闻标签
     * @param position                      标签下标
     */
    public void selectNewsLable(NewsListRecyclerView newsListView, int position) {
        newsModel.getNewsLable(position, (mainLable, childLables) -> {
            HomeView v = getView();
            if( v != null ) v.selectNewsLable(newsListView, mainLable, childLables);
        });
    }

    /**
     * 获取新闻标签列表
     */
    public void getNewsLableList() {
        newsModel.getNewsLableList(map -> {
            HomeView v = getView();
            if( v != null ) v.newsLableList( map );
        });
    }

    public List<View> getNewsPagerList(final List<NewsLableEntity> mainLables) {
        List<View> pageList = new ArrayList<>();
        NewsView newsView;
        NewsListRecyclerView newsListView;
        //加载空页面
        for( int i = 0; mainLables != null && i < mainLables.size(); i++ ) {
            newsView = new NewsView( mContext );         //创建新闻组件
            newsListView = newsView.getNewsListView();   //获取组件中的新闻列表
            //首次创建时先创建第一个页面
            if( i == 0 ) {
                //主标签
                long mainLableId = mainLables.get( i ).getLableId();
                //查询数据库中的数据
                List<NewsListEntity> sqlList = queryNewsListSql( mainLableId );
                //设置数据库的新闻到新闻列表
                if( sqlList != null ) newsListView.setNewsDataList(mainLableId, sqlList);

                /* 请求网络新闻 */
                //设置新闻参数
                newsListView.setNewsParam(
                        new NewsParamEntity()
                                .setMainLableId( mainLableId )  //设置默认主标签
                );
                //请求新闻数据
                newsListView.requestNewsData();
            }
            //加入到页面列表
            pageList.add( newsView );
        }
        return pageList;
    }

    /**
     * 查询数据库新闻列表
     * @param mainLableId   主标签
     * @param offset        开始位置
     * @return              新闻数据列表
     */
    @Nullable
    public List<NewsListEntity> queryNewsListSql(long mainLableId, int offset) {
        List<NewsListEntity> list = mNewsSQL.queryNewsData( mainLableId, offset );
        LogUtils.d("queryNewsListSql",
                "MainLableId:" + mainLableId + " ListSize:" + list.size());
        return list.size() == 0 ? null : list;
    }

    /**
     * 查询数据库新闻列表
     * 只查询默认条数，从0开始
     * @param mainLableId   主标签
     * @return              新闻数据列表
     */
    @Nullable
    public List<NewsListEntity> queryNewsListSql(long mainLableId) {
        return queryNewsListSql(mainLableId, 0);
    }

    /**
     * 插入数据库新闻列表
     * @param mainLableId   主标签
     * @param childLableId  子标签
     * @param listData      新闻数据
     */
    public void insertNewsListSqlList(long mainLableId, long childLableId,
                                       List<NewsListEntity> listData) {
        LogUtils.d("insertNewsListSqlList",
                "MainLableId:" + mainLableId +
                        " ChildLableId:" +  childLableId +
                        " ListSize:" + listData.size());
        if( listData.size() == 0 ) return;
        mNewsSQL.insertNewsData(mainLableId, childLableId, listData);
    }
}